if SERVER then
	AddCSLuaFile("shared.lua")
end

if CLIENT then
	SWEP.PrintName = "Hands"
	SWEP.Slot = 1
	SWEP.SlotPos = 0
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false
end

SWEP.Author = "Wantor"
SWEP.Instructions = "Left click to punch. Right click to lock/unlock a door. Reload to buy/sell a door"
SWEP.Contact = ""
SWEP.Purpose = ""

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

local PunchSounds = {
"punch/punch_01.wav",
"punch/punch_02.wav",
"punch/punch_03.wav",
"punch/punch_04.wav",
"punch/punch_05.wav",
"punch/punch_06.wav",
"punch/punch_07.wav",
"punch/punch_08.wav",
"punch/punch_09.wav",
"punch/punch_10.wav"
}

SWEP.KeySound = Sound("doors/door_latch3.wav")
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = ""

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = ""

function SWEP:Initialize()
	if SERVER then self:SetWeaponHoldType("normal") end
end

function SWEP:Deploy()
	if CLIENT then return end
	self.Owner:DrawViewModel(false)
	self.Owner:DrawWorldModel(false)
end

function SWEP:PrimaryAttack()
	if CLIENT then return end
	
	local tgt = self.Owner:GetEyeTrace().Entity
	local PunchSound = PunchSounds[math.random(#PunchSounds)]
	if tgt:IsValid() and tgt:IsPlayer() and self.Owner:EyePos():Distance(tgt:GetPos()) < 100 then
		tgt:SetVelocity(Vector(math.random(-200,200),math.random(-200,200),math.random(150,200)))
		tgt:TakeDamage(math.random(4,11), self.Owner, self.Owner)
		tgt:EmitSound(PunchSound)
		self:SetNextPrimaryFire(CurTime() + 1.5)
	end
end

function SWEP:SecondaryAttack()
	if CLIENT then return end
	local ent = self.Owner:GetEyeTrace().Entity

	if ent:IsValid() and ent:Door() and self.Owner:EyePos():Distance(ent:GetPos()) < 85 then
	
		if ent:DoorOwner() == self.Owner then
		
			if ent:GetNWBool("locked") == true and ent:DoorOwner() == self.Owner then
			
				ent:Fire("unlock", "", 0)
				self.Owner:EmitSound(self.KeySound)
				ent:SetNWBool("locked", false)
				
			elseif ent:GetNWBool("locked") == false and ent:DoorOwner() == self.Owner then
			
				ent:Fire("lock", "", 0)
				self.Owner:EmitSound(self.KeySound)
				ent:SetNWBool("locked", true)
				
			end
			
			self:SetNextSecondaryFire(CurTime() + 0.5)
			
		else
		
			self.Owner:EmitSound("physics/wood/wood_crate_impact_hard3.wav", 100, 100)
			timer.Simple(0.2, function()
			self.Owner:EmitSound("physics/wood/wood_crate_impact_hard3.wav", 100, 100)
			end)
			timer.Simple(0.4, function()
			self.Owner:EmitSound("physics/wood/wood_crate_impact_hard3.wav", 100, 100)
			end)
			self:SetNextSecondaryFire(CurTime() + 1.5)
			
		end
	end
end

SWEP.active = false
function SWEP:Reload()
	if CLIENT then return end
	if !self.active then

		self.active = true
			
		local ent = self.Owner:GetEyeTrace().Entity
		
		if ent:IsValid() and ent:Door() and self.Owner:EyePos():Distance(ent:GetPos()) < 75 then
		
			if ent:GetNWBool("doorowned") == false then
			
				ent:OwnDoor(self.Owner)
				
			elseif ent:GetNWBool("doorowned") == true and ent:DoorOwner() == self.Owner then
			
				ent:SellDoor(self.Owner)
				
			end
			
			timer.Simple(3, function()
			
				self.active = false
				
			end)
			
		end
		
	end
	
end
